package screens;

import com.alee.laf.panel.WebPanel;
import com.googlecode.charts4j.*;
import controller.App;
import controller.Query;
import model.UserModel;
import sql.Db;
import sql.ResultSetHandler;
import util.FontLoader;
import view.ChartView;
import view.StatView;
import view.UserView;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class FriendsListScreen extends WebPanel {
    private static final long serialVersionUID = 5469633600652599571L;

    /**
     * Shows the list of a users friends and statistics about that friend list.
     */
    public FriendsListScreen() {
        this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
        this.setOpaque(false);

        getFriends();
    }

    /**
     * Get all friends
     */
    public void getFriends() {
        JLabel v = new JLabel("Friends");
        Font f = FontLoader.getFont(FontLoader.OPENSANS_BOLD);
        f = f.deriveFont(26f);
        v.setAlignmentX(Component.CENTER_ALIGNMENT);
        v.setForeground(java.awt.Color.DARK_GRAY);
        v.setFont(f);
        add(v);
        add(Box.createVerticalStrut(20));

        ArrayList<UserModel> users = Query.getFriends(App.getCurrentUser().userId);
        showUsers(users);
    }
    
    public void showUsers(final ArrayList<model.UserModel> users) {
        final WebPanel friendsGrid = new WebPanel();
        friendsGrid.setAlignmentX(Component.CENTER_ALIGNMENT);
        friendsGrid.setOpaque(false);
        friendsGrid.setLayout(new GridLayout(0, 2, 10, 10));
        friendsGrid.setMargin(0,0,50, 0);

        final JButton loadMore = new JButton("Load more...");
        final JLabel showing = new JLabel("Showing");

        add(friendsGrid);
        add(showing);
        add(loadMore);

        loadMore.setAlignmentX(Component.CENTER_ALIGNMENT);
        showing.setAlignmentX(Component.CENTER_ALIGNMENT);
        friendsGrid.setAlignmentX(Component.CENTER_ALIGNMENT);
        add(Box.createRigidArea(new Dimension(0,50)));


        // add first 10
        int i;
        for (i = 0; i < 10 && i < users.size(); i++) {
            friendsGrid.add(new UserView(users.get(i)));
        }
        if (i == users.size()) {
            remove(loadMore);
        }

        showing.setText("Showing " + i + " of " + users.size());

        // load more event handler
        loadMore.addActionListener(new ActionListener() {
            int pageSize = 50;
            int itemCount = users.size();
            int currentIndex = 10;
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                int i;
                for (i = currentIndex; i < currentIndex + pageSize && i < itemCount; i++) {
                    friendsGrid.add(new UserView(users.get(i)));
                }
                currentIndex = i;
                revalidate();
                loadMore.setText("Load more...");
                loadMore.setEnabled(true);
                showing.setText("Showing " + i + " of " + users.size());

                if (currentIndex == itemCount) {
                    remove(loadMore);
                }
            }
        });
    }
}
